library("RTextTools")
library("RWeka")
library("tm")
library("gdata")

source("trab-funcoes-fernando.r")

# Funcoes que tokenizam o texto nos gramas desejados

BigramTokenizer = function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2));
TrigramTokenizer = function(x) NGramTokenizer(x, Weka_control(min = 3, max = 3));
QuadgramTokenizer = function(x) NGramTokenizer(x, Weka_control(min = 4, max = 4));



### MAIN ###
dirPath <- './data';
corpus = Corpus(DirSource(directory=dirPath, pattern="treino.txt"));
corpus = tm_map(x=corpus, tolower);
corpus = tm_map(x=corpus, removeNumbers);

# O dicionario e os classificadores sao matrizes de n x 2, contendo o n-grama na primeira coluna e a ocorrencia na segunda

# Dicionario de palavras e sua frequencia
dictionary = as.matrix(TermDocumentMatrix(x=corpus, control = list(tokenize = WordTokenizer)));

# Classificador bigrama
bigramTable = as.matrix(TermDocumentMatrix(x=corpus, control = list(tokenize = BigramTokenizer)));

# Classificador trigrama
trigramTable = as.matrix(TermDocumentMatrix(x=corpus, control = list(tokenize = TrigramTokenizer)));

# Classificador quadgrama
quadgramTable = as.matrix(TermDocumentMatrix(x=corpus, control = list(tokenize = QuadgramTokenizer)));



bigram=gramSimplifier(bigramTable,2)
trigram=gramSimplifier(trigramTable,3)
quadgram=gramSimplifier(quadgramTable,4)

corpus=Corpus(DirSource(directory=dirPath, pattern="validacao.txt"))
quadgramTestTable=as.matrix(TermDocumentMatrix(x=corpus, control = list(tokenize = QuadgramTokenizer)))

corpus=Corpus(DirSource("./data/frases/"))

cov=getCovarianceMatriz(dictionary,corpus,"./data/english")
history_cor=as.vector(rep(c(0),nrow(c)))
names(history_cor)=rownames(c)

e=errorEvaluator(dictionary,bigram,trigram,quadgram,quadgramTestTable,cor_mat=cov,history_cor=history_cor)

